﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using NETL.Engine.Source;
using NETL.Engine.Destination;
using NETL.Engine.Transformation;
using NETL.Data.Config;
using System.IO;
using NETL.Engine.Util;

namespace NETL.Engine.Context
{
	public class FactoryLoader
	{
		private static FactoryLoader myInstance = new FactoryLoader();

		private List<ISourceFactory> mySourceFactories = new List<ISourceFactory>();
		private List<IDestinationFactory> myDestinationFactories = new List<IDestinationFactory>();
		private List<ITransformationFactory> myTransformationFactories = new List<ITransformationFactory>();

		public static FactoryLoader Instance
		{
			get { return myInstance; }
		}

		private FactoryLoader()
		{





			DirectoryInfo dInfo = new DirectoryInfo(FileUtil.BaseDirectory);

			FileInfo[] files = dInfo.GetFiles("*.dll");

			foreach (FileInfo fInfo in files)
			{
				Assembly asm = Assembly.LoadFrom(fInfo.FullName);

				List<Type> types = new List<Type>(asm.GetExportedTypes());
				List<Type> sourceFactories = types.FindAll(delegate(Type t) { return t.IsSubclassOf(typeof(ISourceFactory)); });
				List<Type> destinationFactories = types.FindAll(delegate(Type t) { return t.IsSubclassOf(typeof(IDestinationFactory)); });
				List<Type> transformationFactories = types.FindAll(delegate(Type t) {return t.IsSubclassOf(typeof(ITransformationFactory)); });

				foreach (Type t in sourceFactories)
				{
					mySourceFactories.Add(Activator.CreateInstance(t) as ISourceFactory);
				}

				foreach (Type t in destinationFactories)
				{
					myDestinationFactories.Add(Activator.CreateInstance(t) as IDestinationFactory);
				}

				foreach (Type t in transformationFactories)
				{
					myTransformationFactories.Add(Activator.CreateInstance(t) as ITransformationFactory);
				}

			}
			
		}

		public IMessageDestination BuildDestination(DestinationData data)
		{
			throw new NotImplementedException();
		}

		public IMessageSource BuildSource(SourceData data)
		{
			throw new NotImplementedException();
		}

		public ITransformation BuildTransformation(TransformationData data)
		{
			throw new NotImplementedException();
		}
	}
}
